package com.youkeyi.ddy.cloud.basic.storage.vaccinum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.storage.vaccinum.entity.TblGoodsVaccinum;
import com.youkeyi.ddy.cloud.basic.storage.vaccinum.service.TblGoodsVaccinumService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.StorageVaccinumCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.dto.StorageVaccinumListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.dto.StorageVaccinumListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.vaccinum.param.StorageVaccinumUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗商品表 前端控制器
 * </p>
 *
 * @vaccinumor xiawang.liu
 * @since 2024-03-25
 */
@Service
public class TblGoodsVaccinumController implements StorageVaccinumCloudService {

    @Autowired
    private TblGoodsVaccinumService tblGoodsVaccinumService;

    @Override
    public StorageVaccinumListCloudDto query(StorageVaccinumQueryCloudParam param) {
        StorageVaccinumListCloudDto dto = new StorageVaccinumListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getVaccinumPrimaryKeys())) {
            param.getVaccinumPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBarCodes())) {
            param.getBarCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getQrCodes())) {
            param.getQrCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getVaccinationPopulations())) {
            param.getVaccinationPopulations().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblGoodsVaccinum> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getVaccinumPrimaryKeys()), TblGoodsVaccinum::getPrimaryKey, param.getVaccinumPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), TblGoodsVaccinum::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getBarCodes()), TblGoodsVaccinum::getBarCode, param.getBarCodes())
                .in(CollectionUtils.isNotEmpty(param.getQrCodes()), TblGoodsVaccinum::getQrCode, param.getQrCodes())
                .in(CollectionUtils.isNotEmpty(param.getVaccinationPopulations()), TblGoodsVaccinum::getVaccinationPopulation, param.getVaccinationPopulations())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), TblGoodsVaccinum::getName, param.getNameFuzzy())
                .like(StringUtils.isNotBlank(param.getKeyWordFuzzy()), TblGoodsVaccinum::getKeyWord, param.getKeyWordFuzzy())
                .ne(TblGoodsVaccinum::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblGoodsVaccinum> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblGoodsVaccinum> vaccinumList = tblGoodsVaccinumService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(vaccinumList.getRecords())) {
            dto.setTotal(vaccinumList.getTotal());

            List<StorageVaccinumListItemCloudDto> items = new ArrayList<>();
            vaccinumList.getRecords().forEach(vaccinum -> {
                StorageVaccinumListItemCloudDto item = new StorageVaccinumListItemCloudDto();
                item.setVaccinumPrimaryKey(vaccinum.getPrimaryKey());
                item.setCorpKey(vaccinum.getCorpKey());
                item.setName(vaccinum.getName());
                item.setAliasName(vaccinum.getAliasName());
                item.setKeyWord(vaccinum.getKeyWord());
                item.setCategoryKey(vaccinum.getCategoryKey());
                item.setCategoryName(vaccinum.getCategoryName());
                item.setVaccinationPopulation(vaccinum.getVaccinationPopulation());
                item.setVaccinationMethod(vaccinum.getVaccinationMethod());
                item.setPrices(vaccinum.getPrices());
                item.setCost(vaccinum.getCost());
                item.setSaveEnvironment(vaccinum.getSaveEnvironment());
                item.setValidityPeriod(vaccinum.getValidityPeriod());
                item.setVaccinationFrequency(vaccinum.getVaccinationFrequency());
                item.setPlanOne(vaccinum.getPlanOne());
                item.setPlanTwo(vaccinum.getPlanTwo());
                item.setPlanThree(vaccinum.getPlanThree());
                item.setPlanFour(vaccinum.getPlanFour());
                item.setPlanFive(vaccinum.getPlanFive());
                item.setBarCode(vaccinum.getBarCode());
                item.setQrCode(vaccinum.getQrCode());
                item.setThirdPartyIdentificationKey(vaccinum.getThirdPartyIdentificationKey());
                item.setIdentification(vaccinum.getIdentification());
                item.setPath(vaccinum.getPath());
                item.setManufacturer(vaccinum.getManufacturer());
                item.setProductionLicense(vaccinum.getProductionLicense());
                item.setRemark(vaccinum.getRemark());
                item.setIsDeleted(vaccinum.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(vaccinum.getCreateTime() != null ? vaccinum.getCreateTime().getTime() : null);
                item.setCreateUserKey(vaccinum.getCreateUserKey());
                item.setCreateUserName(vaccinum.getCreateUserName());
                item.setUpdateTime(vaccinum.getUpdateTime() != null ? vaccinum.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(vaccinum.getUpdateUserKey());
                item.setUpdateUserName(vaccinum.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(StorageVaccinumCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getName())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblGoodsVaccinum vaccinum = new TblGoodsVaccinum();
        vaccinum.setPrimaryKey(SimpleUUidUtils.getUuid());
        vaccinum.setCorpKey(param.getCorpKey());
        vaccinum.setName(param.getName());
        vaccinum.setAliasName(param.getAliasName());
        vaccinum.setKeyWord(param.getKeyWord());
        vaccinum.setCategoryKey(param.getCategoryKey());
        vaccinum.setCategoryName(param.getCategoryName());
        vaccinum.setVaccinationPopulation(param.getVaccinationPopulation());
        vaccinum.setVaccinationMethod(param.getVaccinationMethod());
        vaccinum.setPrices(param.getPrices());
        vaccinum.setCost(param.getCost());
        vaccinum.setSaveEnvironment(param.getSaveEnvironment());
        vaccinum.setValidityPeriod(param.getValidityPeriod());
        vaccinum.setVaccinationFrequency(param.getVaccinationFrequency());
        vaccinum.setPlanOne(param.getPlanOne());
        vaccinum.setPlanTwo(param.getPlanTwo());
        vaccinum.setPlanThree(param.getPlanThree());
        vaccinum.setPlanFour(param.getPlanFour());
        vaccinum.setPlanFive(param.getPlanFive());
        vaccinum.setBarCode(param.getBarCode());
        vaccinum.setQrCode(param.getQrCode());
        vaccinum.setThirdPartyIdentificationKey(param.getThirdPartyIdentificationKey());
        vaccinum.setIdentification(param.getIdentification());
        vaccinum.setPath(param.getPath());
        vaccinum.setManufacturer(param.getManufacturer());
        vaccinum.setProductionLicense(param.getProductionLicense());
        vaccinum.setRemark(param.getRemark());
        vaccinum.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        vaccinum.setCreateUserKey(param.getCreateUserKey());
        vaccinum.setCreateUserName(param.getCreateUserName());
        vaccinum.setCreateTime(new Date());
        boolean save = tblGoodsVaccinumService.save(vaccinum);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(vaccinum.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(StorageVaccinumUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getVaccinumPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblGoodsVaccinum> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblGoodsVaccinum::getPrimaryKey, param.getVaccinumPrimaryKey());
        TblGoodsVaccinum vaccinum = new TblGoodsVaccinum();
        vaccinum.setPrimaryKey(param.getVaccinumPrimaryKey());
        vaccinum.setName(param.getName());
        vaccinum.setAliasName(param.getAliasName());
        vaccinum.setKeyWord(param.getKeyWord());
        vaccinum.setCategoryKey(param.getCategoryKey());
        vaccinum.setCategoryName(param.getCategoryName());
        vaccinum.setVaccinationPopulation(param.getVaccinationPopulation());
        vaccinum.setVaccinationMethod(param.getVaccinationMethod());
        vaccinum.setPrices(param.getPrices());
        vaccinum.setCost(param.getCost());
        vaccinum.setSaveEnvironment(param.getSaveEnvironment());
        vaccinum.setValidityPeriod(param.getValidityPeriod());
        vaccinum.setVaccinationFrequency(param.getVaccinationFrequency());
        vaccinum.setPlanOne(param.getPlanOne());
        vaccinum.setPlanTwo(param.getPlanTwo());
        vaccinum.setPlanThree(param.getPlanThree());
        vaccinum.setPlanFour(param.getPlanFour());
        vaccinum.setPlanFive(param.getPlanFive());
        vaccinum.setBarCode(param.getBarCode());
        vaccinum.setQrCode(param.getQrCode());
        vaccinum.setThirdPartyIdentificationKey(param.getThirdPartyIdentificationKey());
        vaccinum.setIdentification(param.getIdentification());
        vaccinum.setPath(param.getPath());
        vaccinum.setManufacturer(param.getManufacturer());
        vaccinum.setProductionLicense(param.getProductionLicense());
        vaccinum.setRemark(param.getRemark());
        vaccinum.setUpdateUserKey(param.getUpdateUserKey());
        vaccinum.setUpdateUserName(param.getUpdateUserName());
        vaccinum.setUpdateTime(new Date());
        boolean save = tblGoodsVaccinumService.update(vaccinum, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(StorageVaccinumDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getVaccinumPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblGoodsVaccinum> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblGoodsVaccinum::getPrimaryKey, param.getVaccinumPrimaryKeys());

        TblGoodsVaccinum vaccinum = new TblGoodsVaccinum();
        vaccinum.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        vaccinum.setUpdateUserKey(param.getUpdateUserKey());
        vaccinum.setUpdateUserName(param.getUpdateUserName());
        vaccinum.setUpdateTime(new Date());
        boolean save = tblGoodsVaccinumService.update(vaccinum, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
